/**
 * Created by Administrator on 2017/12/18 0018.
 */
define(['jquery'],function($){
    var common = {};
    common.getHtml = function(defaults){
        $.post(defaults.url,function(html){
            $("#content-main").html(html);
            $("#myModalbox").html("");
        }).error(function(msg){
            if(msg.readyState < 4){//0,1,2,3,4
                $("#content-main").html("<p class='text-center'>网络连接异常</p>");
            }else if(msg.status == 404){
                $("#content-main").html("<p class='text-center'>找不到页面</p>");
            }
        });
    };
    /**
     * 封装Ajax请求
     * 参数与jQuery ajax一致
     */
    common.ajax = function(params){
        params = getAjaxParams(params);
        return $.ajax(params);
    };
    /**
     * 继承ajax请求的默认参数
     */
    var getAjaxParams = function(params){
        var defaultParams = {
            type:"post",
            contentType: "application/json;charset=UTF-8",
            dataType:"json",
            data:JSON.stringify({
                data : {}
            }),
            error: function(data){
                console.log(data);
            }
        };
        return $.extend(defaultParams,params);
    }

    /*
    * 提示框：
    * */
    common.toastShow = function(msg,time,callback,params){
        var className = "toast-showMessage";
        var confirmClassName = "toast-confirm";
        $("."+className).remove();
        time = time ? time : 3500;
        var h = '<div class="'+className+'">' + msg + '</div>';
        $("body").append(h);
        var left = (- parseInt($("."+className).outerWidth() / 2) ) + 'px';
        var top = (- parseInt($("."+className).outerHeight() / 2) ) + 'px';
        $("."+className).css({
            marginLeft: left,
            marginTop: top,
        });
        timer = setTimeout(function(){
            $("."+className).remove();
        }, time);
        if(callback && typeof(callback) === "function"){
            setTimeout(function(){
                callback(params);
            }, time);
        }
    };
    //返回当前时间：
    common.getNowFormatDate=function(){
        var date = new Date();
        var seperator1 = "-";
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate;
        return currentdate;
    };
    //计算天数差的函数，通用 ：
    common.dateDiff=function(sDate1,  sDate2){
        var  aDate,  oDate1,  oDate2,  iDays;
        aDate  =  sDate1.split("-");
        oDate1  =  new  Date(parseFloat(aDate[0]),parseFloat(aDate[1])-1,parseFloat(aDate[2]));    //创建时间；
        aDate  =  sDate2.split("-");
        oDate2  =  new  Date(parseFloat(aDate[0]),parseFloat(aDate[1])-1,parseFloat(aDate[2]));    //创建时间；
        iDays  =  parseInt(Math.abs(oDate1  -  oDate2)  /  1000  /  60  /  60  /24);    //把相差的毫秒数转换为天数
        return  iDays
    };
    common.dateDiff2=function(sDate1,  sDate2){
        var  aDate,  oDate1,  oDate2,  iDays;
        aDate  =  sDate1.split("-");
        oDate1  =  new  Date(parseFloat(aDate[0]),parseFloat(aDate[1])-1,parseFloat(aDate[2]));    //创建时间；
        aDate  =  sDate2.split("-");
        oDate2  =  new  Date(parseFloat(aDate[0]),parseFloat(aDate[1])-1,parseFloat(aDate[2]));    //创建时间；
        iDays  =  parseInt((oDate1  -  oDate2)  /  1000  /  60  /  60  /24);    //把相差的毫秒数转换为天数
        return  iDays
    };
    return common;
});
